package com.example.hlddz2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.hlddz2.R;
import java.util.ArrayList;
import java.util.List;

public class AddressActivity extends AppCompatActivity {
    private RecyclerView rvAddress;
    private AddressAdapter adapter;
    private List<Address> addressList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);

        // 初始化数据
        initData();
        // 初始化列表
        initRecyclerView();
        // 添加地址按钮点击事件
        findViewById(R.id.btn_add_address).setOnClickListener(v -> {
            Toast.makeText(this, "跳转到添加地址页面", Toast.LENGTH_SHORT).show();
            // 实际项目中应跳转到地址编辑页面
        });
    }

    private void initData() {
        addressList = new ArrayList<>();
        // 模拟地址数据
        addressList.add(new Address("张三", "13800138000", "北京市朝阳区XX街道XX小区3号楼2单元101"));
        addressList.add(new Address("李四", "13900139000", "上海市浦东新区XX路XX号502室"));
    }

    private void initRecyclerView() {
        rvAddress = findViewById(R.id.rv_address);
        rvAddress.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddressAdapter(addressList,
                // 编辑地址回调
                position -> {
                    Address address = addressList.get(position);
                    Toast.makeText(this, "编辑：" + address.name, Toast.LENGTH_SHORT).show();
                },
                // 删除地址回调
                position -> showDeleteDialog(position)
        );
        rvAddress.setAdapter(adapter);
    }

    // 显示删除确认对话框
    private void showDeleteDialog(int position) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定要删除这个地址吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    addressList.remove(position);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(this, "地址已删除", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    // 地址实体类
    static class Address {
        String name;
        String phone;
        String address;

        public Address(String name, String phone, String address) {
            this.name = name;
            this.phone = phone;
            this.address = address;
        }
    }

    // 地址适配器
    static class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {
        private List<Address> data;
        private OnEditListener editListener;
        private OnDeleteListener deleteListener;

        public AddressAdapter(List<Address> data, OnEditListener editListener, OnDeleteListener deleteListener) {
            this.data = data;
            this.editListener = editListener;
            this.deleteListener = deleteListener;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(parent.getContext(), R.layout.item_address, null);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Address address = data.get(position);
            holder.tvName.setText(address.name);
            // 手机号中间4位隐藏
            holder.tvPhone.setText(hidePhone(address.phone));
            holder.tvAddress.setText(address.address);

            // 编辑点击事件
            holder.tvEdit.setOnClickListener(v -> editListener.onEdit(position));
            // 删除点击事件
            holder.tvDelete.setOnClickListener(v -> deleteListener.onDelete(position));
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        // 隐藏手机号中间4位
        private String hidePhone(String phone) {
            if (phone.length() == 11) {
                return phone.substring(0, 3) + "****" + phone.substring(7);
            }
            return phone;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvName, tvPhone, tvAddress, tvEdit, tvDelete;

            ViewHolder(@NonNull View itemView) {
                super(itemView);
                tvName = itemView.findViewById(R.id.tv_name);
                tvPhone = itemView.findViewById(R.id.tv_phone);
                tvAddress = itemView.findViewById(R.id.tv_address);
                tvEdit = itemView.findViewById(R.id.tv_edit);
                tvDelete = itemView.findViewById(R.id.tv_delete);
            }
        }

        // 编辑回调接口
        interface OnEditListener {
            void onEdit(int position);
        }

        // 删除回调接口
        interface OnDeleteListener {
            void onDelete(int position);
        }
    }
}